package com.yisu.expressway.onedollar.presenter.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.onedollar.model.BannerNoticeObj;
import com.yisu.expressway.onedollar.model.BuyNumConfigObj;
import com.yisu.expressway.onedollar.model.GoodsItem;
import com.yisu.expressway.onedollar.model.NoticeItem;
import com.yisu.expressway.onedollar.model.ODBannerItem;
import com.yisu.expressway.onedollar.model.OrderObj;
import com.yisu.expressway.onedollar.model.SalesGoodObj;
import com.yisu.expressway.onedollar.presenter.IOneDollarMainPresenter;
import com.yisu.expressway.onedollar.view.IOneDollarMainView;

import android.content.Context;

/**
 * Created by Kaven on 2016/12/7.
 */

public class OneDollarMainPresenterImpl implements IOneDollarMainPresenter {
    private Context mContext;
    private IOneDollarMainView mView;
    private BannerNoticeObj mBannerNoticeObj;
    private SalesGoodObj mSalesGoodObj;

    public OneDollarMainPresenterImpl(Context context, IOneDollarMainView iOneDollarMainView) {
        mContext = context;
        mView = iOneDollarMainView;
    }

    @Override
    public void init() {
        mView.showLoadingDlg();
        getBannerNotice();
    }

    private void getBannerNotice() {
        ApiClientHelper.postApi(ApiSetting.querySaleGoodsBannerInfo(),
                new TypeToken<BannerNoticeObj>() {
                }, new JSONObject(), new Response.Listener<ApiResult<BannerNoticeObj>>() {
                    @Override
                    public void onResponse(ApiResult<BannerNoticeObj> result) {
                        if (!result.success) {
                            mView.showToast(result.getMsg());
                            mView.refreshViews(false);
                            return;
                        }
                        mBannerNoticeObj = result.getResult();
                        mView.refreshViews(true);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        mView.showToast(volleyError.getMessage());
                        mView.refreshViews(false);
                    }
                }, mContext);
    }

    private void getSalesGoods(int start, int pageSize) {
        if (pageSize <= 0) {
            return;
        }
        Map<String, Object> params = new HashMap();

        int pageIndex = getPageIndex(start, pageSize);

        params.put("pageindex", pageIndex);
        params.put("pagesize", pageSize);
        ApiClientHelper.postApi(ApiSetting.querySaleGoodsList(), new TypeToken<SalesGoodObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<SalesGoodObj>>() {
            @Override
            public void onResponse(ApiResult<SalesGoodObj> result) {
                if (!result.success) {
                    mView.showToast(result.getMsg());
                    return;
                }
                mSalesGoodObj = result.getResult();
                mView.refreshCommodityList(true, null);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mView.showToast(volleyError.getMessage());
                mView.refreshCommodityList(false, volleyError.getMessage());
            }
        }, mContext);
    }

    private int getPageIndex(int start, int pageSize) {
        int round = start / pageSize;

        return round + 1;
    }

    public List<ODBannerItem> getBannerList() {
        return mBannerNoticeObj == null ? null : mBannerNoticeObj.banners;
    }

    @Override
    public List<NoticeItem> getNoticeList() {
        return mBannerNoticeObj == null ? null : mBannerNoticeObj.notices;
    }

    @Override
    public List<GoodsItem> getCommodityList() {
        return mSalesGoodObj == null ? null : mSalesGoodObj.saleGoods;
    }

    @Override
    public void fetchMore(int start, int pageSize) {
        getSalesGoods(start, pageSize);
    }

    @Override
    public void createOrder(long barCode, final String goodsName, final long periodsNum, final double orderAmount, final long joinNum, final long goodsNo) {
        Map<String, Object> params = new HashMap();
        params.put("periodsNum", periodsNum);
        params.put("barcode", barCode);
        params.put("joinNum", joinNum);
        ApiClientHelper.postApi(ApiSetting.createOrder(), new TypeToken<OrderObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<OrderObj>>() {
            @Override
            public void onResponse(ApiResult<OrderObj> result) {
                if (!result.success) {
                    mView.showToast(result.getMsg());
                    return;
                }
                mView.jump2PayPage(goodsName, orderAmount, result.getResult(),periodsNum,joinNum,goodsNo);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mView.showToast(volleyError.getMessage());
            }
        }, mContext);
    }
}
